package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.SelectCourse;
import com.qf.entity.Student;
import com.qf.service.SelectCourseService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author Hulboy
 * @date 2024/8/23
 * @desc --- 代码敲烂 月薪过万 ---
 */
@RestController
@CrossOrigin
@RequestMapping("/course")
public class SelectCourseController {
    @Autowired
    private SelectCourseService service; // 类名Service

    @GetMapping("/findAll3")
    public R findAll3(@RequestParam HashMap<String, String> map) {
        System.out.println("  开始  ");
        System.out.println("  开始  ");
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));

        System.out.println("pageSize = " + pageSize);
        Integer cid = Integer.valueOf(map.get("cid"));
        PageHelper.startPage(pageNum, pageSize);
        List<Student> list = service.findAll3(cid);
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        // R是返回值类型
        // List<SelectCourse> list = service.findAll3(map);
        // PageInfo<SelectCourse> info = new PageInfo<>(list);
        System.out.println("  结束  ");
        System.out.println("  结束  ");
        // return R.ok(info);
        return R.ok(pageInfo);
    }






    @GetMapping("/findAll2")
    public R findAll2(@RequestParam HashMap<String, String> map) {
        System.out.println("查询数据库是否已有重复数据  开始  ");
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);

        // R是返回值类型

        List<SelectCourse> list = service.findAll2(map);
        PageInfo<SelectCourse> info = new PageInfo<>(list);
        System.out.println("查询数据库是否已有重复数据  结束  结果如下");
        return R.ok(info);
    }


    @GetMapping("/findAll")
    public R findAll(@RequestParam HashMap<String, String> map) {
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);

        // R是返回值类型

        List<SelectCourse> list = service.findAll(map);
        PageInfo<SelectCourse> info = new PageInfo<>(list);
        return R.ok(info);
    }





    @PostMapping("/addCourse")
    public R addCourse(@RequestBody SelectCourse course) {
        System.out.println("打印course = " + course);   // 接收到一行？
        boolean isOk = service.addCourse(course);

        System.out.println("isOk = " + isOk);
        return R.ok(isOk);
    }

    @PostMapping("/edit")
    public R edit(@RequestBody SelectCourse course) {
        System.out.println("打印course = " + course);   // 接收到一行？
        boolean isOk = service.edit(course);
        System.out.println("isOk = " + isOk);
        return R.ok(isOk);
    }

    @GetMapping("/delete")
    public R deleteById(int id) {
        System.out.println("删除这些：" + id);
        boolean isOk = service.deleteById(id);
        return R.ok(isOk);
    }


    @GetMapping("/deletestudent")
    public R deletestudent(@RequestParam HashMap<String, String> map){
        Integer sid = Integer.valueOf(map.get("sid"));
        Integer cid = Integer.valueOf(map.get("cid"));
        int i = service.deletestudent(sid,cid);
        if (i > 0){
            return R.ok();
        }
        return R.fail("删除失败");
    }




}
